//
//  MerchantsApplyForVC.m
//  XY
//
//  Created by 洛水寒 on 16/5/11.
//  Copyright © 2016年 洛水寒. All rights reserved.
//

#import "MerchantsApplyForVC.h"
#import "MerchantCardVC.h"
#import "NetWorkTools.h"
@interface MerchantsApplyForVC ()<UITextFieldDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF2;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *cardTF;
@property (weak, nonatomic) IBOutlet UITextField *dealPswTF;

@end

@implementation MerchantsApplyForVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"申请开店";
    self.edgesForExtendedLayout = UIRectEdgeNone;//跳过64
    self.view.backgroundColor = [UIColor whiteColor];
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];//去掉返回的字
//    self.navigationItem.backBarButtonItem = item;
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(screenBtnClick)];
//    

    
    _phoneTF.delegate =self;
    _pswTF.delegate =self;
    _pswTF2.delegate =self;
    _nameTF.delegate =self;
    _cardTF.delegate =self;
 _dealPswTF.delegate =self;
}
-(void)screenBtnClick
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//下一步
- (IBAction)nextStepBtn:(id)sender
{
    
  
    if ([self.pswTF.text length] && [self.phoneTF.text length] && [self.pswTF2.text length] && [self.nameTF.text length] && [self.cardTF.text length] && [self.dealPswTF.text length]) {
        
        if (![_pswTF.text isEqualToString:_pswTF2.text]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次输入的密码不同" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        }else if (_pswTF.text.length == 0){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        }else{
            //    MerchantCardVC *merchantCardVC = [[MerchantCardVC alloc]init];
            //    [self.navigationController pushViewController:merchantCardVC animated:YES];
            
            
            
            NSDictionary *params = @{
                                     @"UserName":_nameTF.text,
                                     @"UserPassword":_pswTF.text,
                                     @"RealName":_nameTF.text,
                                     @"IsNeedResetPwd":@false,
                                     @"DiscountLimit":@"0",
                                     @"EntryStatus":@"1",
                                     @"IsActive":@true,
                                     @"OwnerId":@"Service",
                                     @"ModifierId":@"Service",
                                     @"LoginCount":@"0",
                                     @"SGrade":@"0",
                                     @"CompanyId":@"-1",
                                     @"DistributorId":@"-1",
                                     @"AgentNo":@"123",
                                     @"Email":_dealPswTF.text,   //交易密码
                                     @"Mobile":_phoneTF.text,
                                     @"WANGWANG":_cardTF.text,    //身份证号
                                     @"Id":@"0",
                                     //                                 @"IsAdmin":@false,
                                     //                                 @"IpRule":@"",
                                     //                                 @"DepartmentId":@"",
                                     //                                 @"PostId":@"",
                                     //                                 @"LeaderId":@"",
                                     //                                 @"Ad_Client_Id":@"",
                                     //                                 @"EntryTime":@"2016-05-18T14: 05: 34.3733065+08: 00",
                                     //                                 @"LeaveTime":@"",
                                     //                                 @"IsActive":@true,
                                     //                                 @"CreationDate":@"2016-05-18T14: 05: 34.3733065+08: 00",
                                     //                                 @"ModifiedDate":@"2016-05-18T14: 05: 34.3733065+08: 00",
                                     //                                 @"LastLoginTime":@"",
                                     //                                 @"LastLoginIP":@"",
                                     //                                 @"AuthStore":@"",
                                     //                                 @"AuthShop":@"",
                                     //                                 @"AuthBrand":@"",
                                     //                                 @"AuthWarehouse":@"W9999",
                                     //                                 @"Logo":@"",
                                     //                                 @"Pic":@"",
                                     //                                 @"lot":@"",
                                     //                                 @"lat":@"",
                                     //                                 @"Parentid":@"",
                                     //                                 @"ByteFileString":@"",
                                     };
            
            
            [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"SYSUSERADD" completeBlock:^(id object) {
                NSLog(@"objectobject%@",object);
                
                NSString *str = [[object objectForKey:@"Id"] stringValue];
                if ([str isEqualToString:@"0"]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = 521;
                    [alert show];
                }else if ([str isEqualToString:@"5"]){
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该用户已存在" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    
                    [alert show];
                }else{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    
                    [alert show];
                    
                }
                
                
                
            }];
            
        }
        
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请您填写完整信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }
 
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 521) {
        [self cleanTextField];
    }
}

- (void)cleanTextField {
    self.phoneTF.text = @"";
    self.pswTF.text = @"";
    self.pswTF2.text = @"";
    self.nameTF.text = @"";
    self.cardTF.text = @"";
    self.dealPswTF.text = @"";
}

/*
 @property (weak, nonatomic) IBOutlet UITextField *phoneTF;
 @property (weak, nonatomic) IBOutlet UITextField *pswTF;
 @property (weak, nonatomic) IBOutlet UITextField *pswTF2;
 @property (weak, nonatomic) IBOutlet UITextField *nameTF;
 @property (weak, nonatomic) IBOutlet UITextField *cardTF;
 @property (weak, nonatomic) IBOutlet UITextField *dealPswTF;
 */


#pragma mark --去键盘

- (void)_handleEndEditing
{
    [self.view endEditing:YES];
}

//当用户按下return去键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
    if (textField == _phoneTF)
        
    {
        
        [_pswTF becomeFirstResponder];
        
    }
    
    if (textField == _pswTF)
        
    {
        
        [_pswTF2 becomeFirstResponder];
        
    }
    if (textField == _pswTF2)
        
    {
        
        [_nameTF becomeFirstResponder];
        
    }
    if (textField == _nameTF)
        
    {
        
        [_cardTF becomeFirstResponder];
        
    }
    if (textField == _cardTF)
        
    {
        
        [_dealPswTF becomeFirstResponder];
        
    }
    if (textField == _dealPswTF)
        
    {
        
        [_dealPswTF resignFirstResponder];
        
    }
    return YES;
    
}



//空白去键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
    [self.view.window endEditing:YES];
    
}


@end
